/**
 * 项目名称: Topic_102.cpp
 * 项目路径: study/DataStructure/LeetCode/树/简单
 * 编辑时间: 2024年08月15日15时00分49秒
 * 用   户: 轻尘
 */

#include <bits/stdc++.h>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    vector<vector<int>> ans;
    vector<int> init;

    void pre(TreeNode *root, int study) {
        if (root) {
            study++;
            int size = ans.size();
            if(size<=study)ans.push_back(init);
            if (root->left) {
                ans[study].push_back(root->left->val);
                pre(root->left, study);
            }
            if (root->right) {
                ans[study].push_back(root->right->val);
                pre(root->right, study);
            }
        }
    }

    vector<vector<int>> levelOrder(TreeNode *root) {
        if (!root)return ans;
        ans.push_back(init);
        ans[0].push_back(root->val);
        pre(root, 0);

        ans.pop_back();
        return ans;
    }
};
